package com.bjsxt.disruptor;

import com.alibaba.fastjson.JSON;
import com.bjsxt.match.MatchServiceFactory;
import com.bjsxt.match.MatchStrategy;
import com.lmax.disruptor.EventHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description:
 * @ClassName: OrderMatchDisruptorHandler
 * @Author: chenyu
 * @Date: 2025/3/20 11:20
 */
@Slf4j
public class OrderMatchDisruptorHandler implements EventHandler<OrderEvent> {

    private OrderBooks orderBooks;

    private String symbol;

    public OrderMatchDisruptorHandler(OrderBooks orderBooks) {
        this.orderBooks = orderBooks;
        this.symbol = orderBooks.getSymbol();
    }

    /**
     * 交给引擎去处理事件
     *
     * @param event
     * @throws Exception
     */


    @Override
    public void onEvent(OrderEvent event, long sequence, boolean endOfBatch) throws Exception {
        //从ringbuffer里面接收了某个数据
        Order order = (Order) event.getSource();
        if (!order.getSymbol().equals(this.symbol))
            return; //接收到了一个不属于我们处理的数据，不处理
        log.info("====> 开始接受委托单{} <======", JSON.toJSONString(order));
        try {
            //处理逻辑
            MatchServiceFactory.getMatchService(MatchStrategy.LIMIT_PRICE).match(orderBooks, order);
        } catch (Exception e) {
            log.error(String.format("处理委托单：%s 发生了错误", JSON.toJSON(order)), e);
        } finally {

        }
    }

    public String getSymbol() {
        return this.symbol;
    }

    public OrderBooks getOrderBooks() {
        return this.orderBooks;
    }
}
